<?php
class ApiService
{
	public function __construct()
	{
		
	}
	
	public function check()
	{
		$apiSetting	= C('SettingService')->get('api_setting',true);
		$apiKey		= $apiSetting['taobao_api_appkey'];
		$keys		= explode(',', $apiKey);
		$num		= count($keys);
		
		$result		= array();
		
		$searchService	= new SearchService();
		$searchService->setSelectRandApi(false);
		for($i=0;$i<$num;$i++)
		{
			$searchService->setSelApi($i);
			
			$params			= array(
				'method' 	=> 'taobao.taobaoke.items.detail.get',
				'fields' 	=> 'iid',
				'nick' 		=> $apiSetting['taobao_api_nick'],
				'pid' 		=> C('CommonFunction')->getPid($apiSetting['taobao_api_pid']),
				'num_iids' 	=> '1',
				'outer_code'	=> 1,
			);
			
			$tmp	= $searchService->getResult($params);
			if(!$tmp)
			{
				$result[$keys[$i]] = array('code'=>-1,'message'=>'未知错误');
				continue;
			}
			if(!array_key_exists('error_response', $tmp))
			{
				$result[$keys[$i]] = array('code'=>0,'message'=>'测试正常');
				continue;
			}
			
			$messages = array('7'=>'应用调用次数超限或者调用频率超限，如果你有足够的空间，建议开启缓存降低调用次数','10'=>'服务不可用','15'=>'远程服务出错,请过一段时间再尝试','25'=>'无效签名,请检查你淘宝客设置中的App Secret是否正确','29'=>'无效的AppKey参数,请检查你淘宝客设置中的App Key是否正确');
			$code	= $tmp['error_response']['code'];
			if(array_key_exists($code,$messages))
			{
				$result[$keys[$i]] = array('code'=>$code,'message'=>$messages[$code]);
			}
			else
			{
				$result[$keys[$i]] = array('code'=>$code,'message'=>$tmp['error_response']['msg']);
			}
		}
		
		return $result;
	}
}